home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1996 September / macformat-041.iso / mac / Shareware City / Graphics / MacSPD / Sources / jacks.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-04-30  |  4.4 KB  |  166 lines  |  [TEXT/MMCC]

  1. /*
  2.  * jacks.c - recursive jack shapes, sort of like a Menger sponge
  3.  *
  4.  * Author:  Alexander Enzmann
  5.  *
  6.  * Modified:
  7.  *
  8.  *      size_factor     # spheres       # squares
  9.  *           x              xx               x
  10.  */
  11.  
  12. #include <stdio.h>
  13. #include <math.h>
  14. #include <stdlib.h>     /* atoi */
  15. #include "def.h"
  16. #include "drv.h"        /* display_close() */
  17. #include "lib.h"
  18.  
  19.  
  20. /* These may be read from the command line */
  21. static int size_factor      = 1;
  22. static int raytracer_format = OUTPUT_RT_DEFAULT;
  23. static int output_format    = OUTPUT_CURVES;
  24.  
  25.  
  26. /* Some basic colors */
  27. static COORD3 Pink    = { 0.737, 0.561, 0.561};
  28. static COORD3 DarkPurple = {0.2, 0.05, 0.2};
  29.  
  30. /* Create a single copy of our recursive object.  The general
  31.    sizing and placement of the object are maintained by the
  32.    recursive routine make_rec_jack.  This routine does the
  33.    actual geometry building */
  34. static void
  35. make_jack_obj()
  36. {
  37.    COORD3 scale;
  38.    COORD4 center, base, apex;
  39.  
  40.    /* Save the current transformation */
  41.    lib_tx_push();
  42.  
  43.       /* Scale the object down to prevent overlaps */
  44.       SET_COORD3(scale, 0.75, 0.75, 0.75);
  45.       lib_tx_scale(scale);
  46.  
  47.       /* Now create the object - three intersecting cylinders
  48.      with spheres at both ends */
  49.  
  50.       /* Cylinders on each axis */
  51.       SET_COORD4(base, -1, 0, 0, 0.1);
  52.       SET_COORD4(apex,  1, 0, 0, 0.1);
  53.       lib_output_cylcone(base, apex, output_format);
  54.       SET_COORD4(base, 0, -1, 0, 0.1);
  55.       SET_COORD4(apex, 0,  1, 0, 0.1);
  56.       lib_output_cylcone(base, apex, output_format);
  57.       SET_COORD4(base, 0, 0, -1, 0.1);
  58.       SET_COORD4(apex, 0, 0,  1, 0.1);
  59.       lib_output_cylcone(base, apex, output_format);
  60.  
  61.       /* Spheres at the ends of the cylinders */
  62.       SET_COORD4(center, 1, 0, 0, 0.2);
  63.       lib_output_sphere(center, output_format);
  64.       SET_COORD4(center, 0, 1, 0, 0.2);
  65.       lib_output_sphere(center, output_format);
  66.       SET_COORD4(center, 0, 0, 1, 0.2);
  67.       lib_output_sphere(center, output_format);
  68.       SET_COORD4(center,-1, 0, 0, 0.2);
  69.       lib_output_sphere(center, output_format);
  70.       SET_COORD4(center, 0,-1, 0, 0.2);
  71.       lib_output_sphere(center, output_format);
  72.       SET_COORD4(center, 0, 0,-1, 0.2);
  73.       lib_output_sphere(center, output_format);
  74.  
  75.    /* Restore the transform to what it was prior to
  76.       entering this routine. */
  77.    lib_tx_pop();
  78. }
  79.  
  80. /* Create a jack shaped object, then put a smaller copy in
  81.    each of the octants defined by the arms of the jack.
  82.    This process is repeated until depth reaches max_depth. */
  83. static void
  84. make_rec_jack PARAMS((int depth, int max_depth))
  85. {
  86.     double i, j, k;
  87.     COORD3 scale, trans;
  88.  
  89.     make_jack_obj();
  90.  
  91.     if (depth < max_depth) {
  92.     SET_COORD3(scale, 0.5, 0.5, 0.5);
  93.     for (i=-0.5;i<=0.5;i+=1)
  94.         for (j=-0.5;j<=0.5;j+=1)
  95.         for (k=-0.5;k<=0.5;k+=1) {
  96.             lib_tx_push();
  97.             SET_COORD3(trans, i, j, k);
  98.             lib_tx_translate(trans);
  99.             lib_tx_scale(scale);
  100.             make_rec_jack(depth+1, max_depth);
  101.             lib_tx_pop();
  102.             }
  103.     }
  104. }
  105.  
  106. int
  107. main PARAMS(( int argc, char *argv[] ))
  108. {
  109.     COORD4 from, at, up;
  110.     COORD4 center;
  111.  
  112.     PLATFORM_INIT(SPD_JACKS);
  113.  
  114.     /* Start by defining which raytracer we will be using */
  115.      if (lib_gen_get_opts(argc, argv, &size_factor,
  116.               &raytracer_format, &output_format))
  117.     return EXIT_FAIL;
  118.  
  119.     /* Set the output file */
  120.     if (lib_open(raytracer_format, "jacks.out"))
  121.     return EXIT_FAIL;
  122.  
  123.     lib_set_polygonalization(2, 2);
  124.  
  125.     /* output background color - Light Grey */
  126.     /* NOTE: Do this BEFORE lib_output_viewpoint(), for display_init() */
  127.     lib_output_background_color(DarkPurple);
  128.  
  129.     /* Viewpoint */
  130.     SET_COORD3(from, 0, 0, -8);
  131.     SET_COORD3(at, 0, 0, 0);
  132.     SET_COORD3(up, 0, 1, 0);
  133.     lib_output_viewpoint(from, at, up, 25.0, 1.0, 0.001, 256, 256);
  134.  
  135.     /* Light */
  136.     SET_COORD4(center, -10, 3, -20, 1);
  137.     lib_output_light(center);
  138.  
  139. #if 0
  140.     /* Declare all the textures we will be using - this way we only
  141.        need to have one set of declarations, rather than creating
  142.        a new texture for every single object */
  143.     allocate_textures();
  144. #endif
  145.  
  146.     /* Save the root transformation */
  147.     lib_tx_push();
  148.  
  149.     /* Rotate everything about the x and y axes */
  150. #if 1
  151.     lib_tx_rotate(X_AXIS,-30 * PI / 180.0);
  152.     lib_tx_rotate(Y_AXIS,-20 * PI / 180.0);
  153. #endif
  154.  
  155.     lib_output_color(NULL, Pink, 0.1, 0.7, 0.7, 0.4, 10.0, 0.0, 1.0);
  156.     make_rec_jack(1, size_factor);
  157.  
  158.     /* Back to where we started */
  159.     lib_tx_pop();
  160.  
  161.     lib_close();
  162.  
  163.     PLATFORM_SHUTDOWN();
  164.     return EXIT_SUCCESS;
  165. }
  166.